home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / round.h < prev    next >
C/C++ Source or Header  |  1995-03-14  |  833b  |  49 lines

  1. /*
  2. ** rounding macros by Dave Knapp, Thad Smith, Jon Strayer, & Bob Stout
  3. */
  4.  
  5. #include <math.h>
  6.  
  7. #if __cplusplus
  8.  
  9. /*
  10. ** Safe C++ inline versions
  11. */
  12.  
  13. /* round to integer */
  14.  
  15. inline int iround(double x)
  16. {
  17.       return (int)floor(x + ((x >= 0) ? 0.5 : -0.5));
  18. }
  19.  
  20. /* round number n to d decimal points */
  21.  
  22. inline double fround(double n, unsigned d)
  23. {
  24.       return floor(n * pow(10., d) + .5) / pow(10., d);
  25. }
  26.  
  27. #else
  28.  
  29. /*
  30. ** NOTE: These C macro versions are unsafe since arguments are referenced
  31. **       more than once.
  32. **
  33. **       Avoid using these with expression arguments to be safe.
  34. */
  35.  
  36. /*
  37. ** round to integer
  38. */
  39.  
  40. #define iround(x) floor((x) + ((x) >= 0 ? 0.5 : -0.5))
  41.  
  42. /*
  43. ** round number n to d decimal points
  44. */
  45.  
  46. #define fround(n,d) (floor((n)*pow(10.,(d))+.5)/pow(10.,(d)))
  47.  
  48. #endif
  49.